package com.blb.springbean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBeanConfig implements BeanPostProcessor {

    @Bean(initMethod = "initMyBean",destroyMethod = "destoryMyBean")
    public MyBean myBean(){
        return new MyBean();
    }

    //初始化前置方法
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if("myBean".equals(beanName)){
            System.out.println("4) 执行BeanPostProcessor的前置方法");
        }
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean,beanName);
    }

    //初始化后置方法
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if("myBean".equals(beanName)){
            System.out.println("7) 执行BeanPostProcessor的后置方法");
        }
        return BeanPostProcessor.super.postProcessAfterInitialization(bean,beanName);
    }
}
